//---------------------------------------------------------------------------

#include <vcl.h>
#pragma hdrstop

#include "cFormBase.h"
//---------------------------------------------------------------------------
#pragma package(smart_init)
#pragma link "JvBalloonHint"
#pragma link "JvButton"
#pragma link "JvComponentBase"
#pragma link "JvExControls"
#pragma link "JvExExtCtrls"
#pragma link "JvExtComponent"
#pragma link "JvGradientHeaderPanel"
#pragma link "JvImage"
#pragma link "JvImageList"
#pragma link "JvMenus"
#pragma link "JvPanel"
#pragma link "JvTransparentButton"
#pragma link "JvVersionControlActions"
#pragma resource "*.dfm"

TfFormBase *fFormBase;
//---------------------------------------------------------------------------
__fastcall TfFormBase::TfFormBase(TComponent* Owner)
	: TForm(Owner)
{
}
//---------------------------------------------------------------------------

void __fastcall TfFormBase::CreateParams(Controls::TCreateParams &Params)
{
	TForm::CreateParams(Params);
	Params.WndParent = GetDesktopWindow();
}
//---------------------------------------------------------------------------
void __fastcall TfFormBase::FormCreate(TObject *Sender)
{
	iShowTimeHint = 1000;
	LastShowHintControl = NULL;
	for(int i=0; i<this->ComponentCount; i++){
		if(this->Components[i]->ClassName() == "TJvTransparentButton" ){
			dynamic_cast<TJvTransparentButton *>(this->Components[i])->OnMouseMove = FormMouseMove;
		}
	}
	

	this->DoubleBuffered = true;
	this->Caption = jvGHPanelTop->LabelCaption;
	this->BorderStyle = bsNone;

	JvImageIcon->Picture->Icon = this->Icon;

	MyNormalWinStateHeight	= this->Height;
	MyNormalWinStateLeft		= this->Left;
	MyNormalWinStateTop			= this->Top;
	MyNormalWinStateWidth		= this->Width;

	RefreshForm(Sender, MyNormal);
}
//---------------------------------------------------------------------------
void __fastcall TfFormBase::actCloseFormExecute(TObject *Sender)
{
	this->Close();
}
//---------------------------------------------------------------------------
void __fastcall TfFormBase::jvGHPanelTopMouseDown(TObject *Sender, TMouseButton Button,
          TShiftState Shift, int X, int Y)
{
	POINT p;
	if(Button == mbLeft && !(this->MyWindowsState == MyMaximizedAllScreen || this->MyWindowsState == MyMaximizedDesktop)){
		ReleaseCapture();
		SendMessage(this->Handle, WM_NCLBUTTONDOWN, HTCAPTION, 0);
	}
	if(Button == mbRight){
		GetCursorPos(&p);
		JvPopupMenuPasekTop->Popup((int)p.x, (int)p.y);
	}
}
//---------------------------------------------------------------------------
void __fastcall TfFormBase::jvGHPanelTopDblClick(TObject *Sender)
{
	if(this->MyWindowsState == MyNormal)
		actMaxForm->Execute();
	else if (this->MyWindowsState == MyMaximizedAllScreen || this->MyWindowsState == MyMaximizedDesktop)
		actNormalForm->Execute();
}
//---------------------------------------------------------------------------

void __fastcall TfFormBase::actMaxFormExecute(TObject *Sender)
{
	if((FindWindow(L"Shell_TrayWnd", NULL) != 0) && !FormMaximizedAllScreen)
		RefreshForm(Sender,MyMaximizedDesktop);
	else
		RefreshForm(Sender,MyMaximizedAllScreen);
}
//---------------------------------------------------------------------------

void __fastcall TfFormBase::actNormalFormExecute(TObject *Sender)
{
	RefreshForm(Sender,MyNormal);
}
//---------------------------------------------------------------------------

void __fastcall TfFormBase::actMinFormExecute(TObject *Sender)
{
	RefreshForm(Sender,MyMinimized);
}
//---------------------------------------------------------------------------

void __fastcall TfFormBase::RefreshForm(TObject *Sender, TMyWindowState WinState)
{
	TRect R;
	TWindowState nWinState;

	MyWindowsState = WinState;
	if(WinState == MyMaximizedAllScreen || WinState == MyMaximizedDesktop){

		tbtnNormal->Visible = true;
		tbtnMax->Visible = false;
		jvPResizeForm->Visible = false;
		jvPResizeForm->Visible = true;
		jvPanelLeft->Visible = false;
		jvPanelRight->Visible = false;
		jvPanelTop->Visible = false;
		jvGHPanelTop->GradientCursor = crDefault;
		jvGHPanelTop->LabelCursor = crDefault;
		jvPanelButton->Visible = false;
		jvPResizeForm->Visible = false;

		if(WinState == MyMaximizedAllScreen)
			nWinState = wsMaximized;
		else{
			//zapamietujemy stary rozmiar okna
			MyNormalWinStateHeight 	= this->Height;
			MyNormalWinStateLeft 		= this->Left;
			MyNormalWinStateTop 		= this->Top;
			MyNormalWinStateWidth 	= this->Width;
			nWinState = wsNormal;
			HMONITOR hmon = MonitorFromWindow(Handle, MONITOR_DEFAULTTONEAREST);
			MONITORINFO mi;
			//FillChar(mi, sizeof(mi), 0);
			mi.cbSize = sizeof(mi);
			GetMonitorInfo(hmon, &mi);
			if((mi.dwFlags && MONITORINFOF_PRIMARY) == 1)
			{
				//SystemParametersInfo(SPI_GETWORKAREA,0,@R,0);
				//Self.Height := Screen.MonitorFromWindow(Handle).Height - (Screen.MonitorFromWindow(Handle).Height - r.Bottom + r.Top);
				this->Height = mi.rcWork.bottom;
			} else
				this->Height = mi.rcMonitor.bottom; // Screen->MonitorFromWindow(Handle)->Height;

			this->Top = mi.rcMonitor.top; //Screen.MonitorFromWindow(Handle).Top;
			this->Left = mi.rcMonitor.left; // Screen.MonitorFromWindow(Handle).Left;
			this->Width = mi.rcMonitor.right - mi.rcMonitor.left; //Screen.MonitorFromWindow(Handle).Width;
		}
	} else {
		tbtnNormal->Visible = false;
		tbtnMax->Visible = true;
		jvPResizeForm->Visible = true;
		jvPanelLeft->Visible = true;
		jvPanelRight->Visible = true;
		jvPanelTop->Visible = true;
//		jvGHPanelTop->GradientCursor = crDefault;
//		jvGHPanelTop->LabelCursor = crDefault;
		jvGHPanelTop->GradientCursor = crSizeAll;
		jvGHPanelTop->LabelCursor = crSizeAll;
		jvPanelButton->Visible = true;
		jvPResizeForm->Visible = true;
		if(WinState == MyMinimized)
			nWinState = wsMinimized;
		else{
			this->Height = MyNormalWinStateHeight;
			this->Left = MyNormalWinStateLeft;
			this->Top = MyNormalWinStateTop;
			this->Width = MyNormalWinStateWidth;
			nWinState = wsNormal;
		}
  }

	jvPResizeForm->Font->Color = jvGHPanelTop->LabelFont->Color;
	this->WindowState = nWinState;
}
//---------------------------------------------------------------------------

void __fastcall TfFormBase::FormMouseMove(TObject *Sender, TShiftState Shift, int X, int Y)
{

	if(LastShowHintControl != Sender){
		LastShowHintControl = Sender;
		JvBalloonHint->CancelHint();
	}
	try{
		if(Sender->ClassName() == "TJvTransparentButton" && !JvBalloonHint->Active)
				JvBalloonHint->ActivateHint(dynamic_cast<TJvTransparentButton *>(Sender),  dynamic_cast<TJvTransparentButton *>(Sender)->Hint, "", iShowTimeHint);
	}catch(...){
		//
	}
}
//---------------------------------------------------------------------------


void __fastcall TfFormBase::jvPResizeFormMouseDown(TObject *Sender, TMouseButton Button, TShiftState Shift, int X, int Y)
{
	if(Button == mbLeft)
	{
		ReleaseCapture();
		this->Perform(Wm_SysCommand, SC_size + 8,0);
	}
}
//---------------------------------------------------------------------------

void __fastcall TfFormBase::JvPanelRightMouseDown(TObject *Sender, TMouseButton Button, TShiftState Shift, int X, int Y)
{
 if(Button == mbLeft){
	ReleaseCapture();
	this->Perform(Wm_SysCommand, SC_size + 2,0);
 }
}
//---------------------------------------------------------------------------

void __fastcall TfFormBase::jvPanelTopMouseDown(TObject *Sender, TMouseButton Button, TShiftState Shift, int X, int Y)
{
	if(Button == mbLeft)
	{
		ReleaseCapture();
		this->Perform(Wm_SysCommand, SC_size + 3,0);
	}
}
//---------------------------------------------------------------------------

void __fastcall TfFormBase::jvPanelButtonMouseDown(TObject *Sender, TMouseButton Button, TShiftState Shift, int X, int Y)
{
	if(Button == mbLeft)
	{
		ReleaseCapture();
		this->Perform(Wm_SysCommand, SC_size + 6,0);
	}
}
//---------------------------------------------------------------------------

void __fastcall TfFormBase::jvPanelLeftMouseDown(TObject *Sender, TMouseButton Button, TShiftState Shift, int X, int Y)
{
	if(Button == mbLeft)
	{
		ReleaseCapture();
		this->Perform(Wm_SysCommand, SC_size + 1,0);
	}
}
//---------------------------------------------------------------------------

